﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace System.Windows
{
    /// <summary>
    /// Implements a utility class for adding and removing elements to and from a <see cref="System.Windows.ResourceDictionary"/>.
    /// </summary>
    public static class ResourceDictionaryExtensions
    {
        /// <summary>
        /// Adds a object to a <see cref="System.Windows.ResourceDictionary"/> instance.
        /// </summary>
        /// <param name="dictionary">The dictionary into which to add the object</param>
        /// <param name="val">The object to add</param>
        public static void Add(this ResourceDictionary dictionary, object val)
        {
            if (val != null)
            {
                if (val is DependencyObject)
                {
                    string name = ElementNaming.Default.NameObject((DependencyObject)val);
                    dictionary.Add(name, val);
                }
            }
        }

        /// <summary>
        /// Removes an object from a <see cref="System.Windows.ResourceDictionary"/> instance.
        /// </summary>
        /// <param name="dictionary">The dictionary from which to remove the object</param>
        /// <param name="val">The object to remove</param>
        public static void Remove(this ResourceDictionary dictionary, object val)
        {
            if (val != null && val is DependencyObject)
            {
                DependencyObject depObject = (DependencyObject)val;
                object name = depObject.GetValue(FrameworkElement.NameProperty);
                if (name != null)
                {
                    if (dictionary.Contains(name)) dictionary.Remove(name.ToString());
                }
            }
        }
    }
}
